package com.store.framework.rest;

import java.util.List;
import java.util.UUID;

import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.store.framework.entry.AppPerTbl;
import com.store.framework.logic.IAppPerLogic;
import com.store.framework.logic.imp.AppPerLogic;

@Path("UserService")
public class UserService {
	IAppPerLogic iAppPerLogic = new AppPerLogic();

	private static Log LOG = LogFactory.getLog(UserService.class);


	// @GET
	// @Produces(MediaType.TEXT_HTML)
	// @Path("addUser")
	@POST
	@Path("addUser")
	@Produces("application/json; charset=UTF-8")
	public String addUser(String input) {
		 LOG.debug(input);
		// LOG.warn("wwwwww");
		// LOG.debug("ddddddddddd");
		// LOG.info("f---f");
		// LOG.error("eee");
		// JSONArray ja = JSONArray.fromObject(input);
		// List<AppPerTbl> list = ja.toList( ja, AppPerTbl.class);
		// for(AppPerTbl at : list){
		//
		// }
		JSONObject jo = JSONObject.fromObject(input);
		@SuppressWarnings("static-access")
		AppPerTbl at = (AppPerTbl) jo.toBean(jo, AppPerTbl.class);
		String key = UUID.randomUUID().toString();
		at.setId(key);

		int row = iAppPerLogic.add(at);
		
		String mess = "";
		if(row==1){
			 mess = "{\"success\":true,\"id\":\""+key+"\"}";
		}else{
			mess = "{\"success\":false}";
		}
		return mess;
	}
	
	@POST
	@Path("deleteUser")
	@Produces("application/json; charset=UTF-8")
	public String deleteUser(String id) {
		AppPerTbl at = new AppPerTbl();
		at.setId(id);
		iAppPerLogic.delete(at);
		return "{\"result\":1}";
	}
	
	@POST
	@Path("updateUser")
	@Produces("application/json; charset=UTF-8")
	public String updateUser(String input) {
		LOG.debug(input);
		JSONObject jo = JSONObject.fromObject(input);
		@SuppressWarnings("static-access")
		AppPerTbl at = (AppPerTbl) jo.toBean(jo, AppPerTbl.class);
		int row = 0;
		String msg = "";
		try {
			row = iAppPerLogic.update(at);
			if(row == 1){
				msg = "{\"success\":true}";
			}
		} catch (Exception e) {
			msg = "{\"success\":false,errorMsg:\""+e.getMessage()+"\"}";
		}
		return msg;
	}

//	@GET
//	@Path("selectApp")start=0&limit=5
//@Produces(MediaType.TEXT_HTML)
	@POST
	@Produces("application/json; charset=UTF-8")
	@Path("selectApp")
	public String selectApp(
			@FormParam("start")Long start,
			@FormParam("limit")Long limit
			) {
		int count = iAppPerLogic.getCount();
		List<AppPerTbl> list = iAppPerLogic.select(start,limit);
		JSONArray ja = JSONArray.fromObject(list);
		LOG.info(ja);
		return "{\"totalProperty\":"+count+",\"root\":"+ja.toString()+"}";
	}

	@GET
	@Produces(MediaType.TEXT_HTML)
	@Path("selectAppByUserId")
	public String selectUserByUserId(String userId) {
		List<AppPerTbl> list = iAppPerLogic.select();
		JSONArray ja = JSONArray.fromObject(list);
		LOG.debug(ja);

		return ja.toString();
	}

	
	@POST
	@Path("error")
	@Produces("application/json; charset=UTF-8")
	public String test(String input) {
		LOG.debug(input);
		
		return " {success: false,msg:'error', errors: {icard:'error'}}";
	}
	
	@POST
	@Path("load")
	@Produces("application/xml; charset=UTF-8")
	public String load(String input) {
		LOG.debug(input);
		String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
		xml += "<message success=\"true\">";
		xml += "<contact>";
		xml += "<name>xxx</name>";
		xml += "</contact>";
		xml += "</message>";
		
		return xml;
	}
	
	public static void main(String[] args) {

		new UserService().deleteUser("xx");
	}
}






